//
//  KYSelectBasicViewController.m
//  kuaiyi
//
//  Created by 杨亮 on 16/3/15.
//  Copyright © 2016年 kuaiyi. All rights reserved.
//

#import "KYSelectBasicViewController.h"
#import "Header.h"
#import "UIBarButtonItem+Extension.h"
#import "UIView+Inspectable.h"
#import "KYDiseaseDetailViewController.h"
#import "KYReadMeViewController.h"
#import "KYSimultaneouslyViewController.h"
#import "KYDiseaseDetailModel.h"
#import "KYSimultaneousModel.h"
#import <SVProgressHUD.h>
#import "KYTreatWayViewController.h"
#import "KYTreatWayModel.h"
#import "KYMatchDoctorCountModel.h"
#import "KYMatchDoctorViewController.h"



@interface KYSelectBasicViewController ()<KYDiseaseDetailViewControllerDelegate,KYSimultaneouslyViewControllerDelegate>
@property (weak, nonatomic) IBOutlet UILabel *diseareLabel;
@property (weak, nonatomic) IBOutlet UIView *viewOne;
@property (weak, nonatomic) IBOutlet UIView *viewFive;
@property (weak, nonatomic) IBOutlet UITextField *diseareTextField;
@property (weak, nonatomic) IBOutlet UITextField *simultanceousTextField;
@property (weak, nonatomic) IBOutlet UITextField *treatWayTextField;


@property (weak, nonatomic) IBOutlet UIButton *buttonOne;
@property (weak, nonatomic) IBOutlet UIButton *littleBtnOne;
@property (weak, nonatomic) IBOutlet UIButton *littleBtnTwo;
@property (weak, nonatomic) IBOutlet UIButton *littleBtnThree;
@property (weak, nonatomic) IBOutlet UIButton *littleBtnFour;
@property (weak, nonatomic) IBOutlet UILabel *countLabel;
@property (weak, nonatomic) IBOutlet UIButton *applyBtn;

@property (nonatomic, strong)KYDiseaseDetailModel *diseaseDetailModel;
@property (nonatomic, strong)NSMutableArray *selectedSims;
@property (nonatomic, strong)KYTreatWayModel *selectedTreatWayModel;
@end

@implementation KYSelectBasicViewController
#pragma mark - 懒加载
-(NSMutableArray *)selectedSims{
    if (!_selectedSims) {
        _selectedSims = [NSMutableArray array];
    }
    return _selectedSims;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpNav];
    [self setUpUI];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(didSelectTreatWay:) name:@"didSelectTreatWay" object:nil];
}
-(void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
#pragma mark - 设置导航栏
-(void)setUpNav{
    UIBarButtonItem *rightText = [UIBarButtonItem setupNavBarBtnWithTitle:@"预约须知" andTarget:self action:@selector(rightBarButtonKlick)];
    self.navigationItem.rightBarButtonItem = rightText;
}
-(void)rightBarButtonKlick{
    KYReadMeViewController *readMeVC = [[KYReadMeViewController alloc]init];
    [self.navigationController pushViewController:readMeVC animated:YES];
}
#pragma mark - 设置UI
-(void)setUpUI{
    //可以用代码,也可以在xib中添加runtime属性
    self.viewOne.cornerRadius = 3;
    self.viewOne.borderWidth = 1;
    self.viewOne.borderColor = [UIColor grayColor];
    [self.buttonOne addTarget:self action:@selector(buttonOneKlick) forControlEvents:UIControlEventTouchUpInside];
    self.littleBtnOne.selected = YES;
    self.littleBtnFour.selected = YES;
    self.viewFive.hidden = YES;
//    self.applyBtn.enabled = NO;
    NSString *diseareText = [NSString stringWithFormat:@"疾病类型:%@疾病",self.diseareText];
    self.diseareLabel.text = diseareText;
    [self getMatchDoctorCount];
}
#pragma mark - 小按钮点击事件
- (IBAction)oneBtnKlick {
    if (self.littleBtnOne.selected) {
        return;
    }else{
        self.littleBtnOne.selected = YES;
        self.littleBtnTwo.selected = NO;
    }
}
- (IBAction)twoBtnKlick {
    if (self.littleBtnTwo.selected) {
        return;
    }else{
        self.littleBtnOne.selected = NO;
        self.littleBtnTwo.selected = YES;
    }
}
- (IBAction)threeBtnKlick {
    
    if (self.littleBtnThree.selected) {
        return;
    }else{
        self.littleBtnThree.selected = YES;
        self.littleBtnFour.selected = NO;
    }
    self.viewFive.hidden = NO;
    self.applyBtn.enabled = NO;
}
- (IBAction)fourBtnKlick {
    
    if (self.littleBtnFour.selected) {
        return;
    }else{
        self.littleBtnFour.selected = YES;
        self.littleBtnThree.selected = NO;
    }
    self.viewFive.hidden = YES;
    if (self.diseareTextField.text.length) {
        self.applyBtn.enabled = YES;
    }
}

#pragma mark - 疾病细分按钮点击
- (IBAction)buttonOneKlick {
    NSLog(@"按钮1被点击了");
    KYDiseaseDetailViewController *diseaseVC = [[KYDiseaseDetailViewController alloc]init];
    [self.navigationController pushViewController:diseaseVC animated:YES];
    diseaseVC.delegate = self;
    diseaseVC.ci1 = self.ci1;
}
#pragma mark - 返回传值的代理方法
-(void)didSelectDiseaseDetail:(KYDiseaseDetailViewController *)diseaseDetailVC diseaseDetail:(KYDiseaseDetailModel *)diseaseDetailModel{
    self.diseareTextField.text = diseaseDetailModel.ci3_name;
    self.diseaseDetailModel = diseaseDetailModel;
    self.simultanceousTextField.text = nil;
}
#pragma mark - 并发症按钮点击
- (IBAction)buttonTwoKlick {
    NSLog(@"按钮2被点击了");
    if (self.diseareTextField.text.length == 0) {
        [SVProgressHUD showErrorWithStatus:@"请选择疾病细分"];
        return;
    }
    KYSimultaneouslyViewController *simVC = [[KYSimultaneouslyViewController alloc]init];
    simVC.delegate = self;
    simVC.selectedModelArray = self.selectedSims;
    [self.navigationController pushViewController:simVC animated:YES];
}
#pragma mark - 代理方法
-(void)didSelectSimultaneous:(KYSimultaneouslyViewController *)simultaneouslyViewController simultaneous:(NSMutableArray *)simultaneous{
    self.selectedSims = simultaneous;
    NSInteger count = simultaneous.count;
    NSString *mutString = [NSString string];
    for (KYSimultaneousModel *model in simultaneous) {
        NSString *string = model.complication_name;
        NSString *s1 = [NSString stringWithFormat:@",%@",string];
        mutString = [mutString stringByAppendingString:s1];
    }
    NSMutableString *ms = [NSMutableString stringWithString:mutString];
    [ms deleteCharactersInRange:NSMakeRange(0, 1)];
    NSString *okString = [NSString stringWithFormat:@"[%@](共%zd条)",ms,count];
    self.simultanceousTextField.text = okString;
}
#pragma mark - 治疗方法按钮点击
- (IBAction)buttonThreeKlick {
    NSLog(@"按钮3被点击了");
    KYTreatWayViewController *treatWayVC = [[KYTreatWayViewController alloc]init];
    treatWayVC.selectedModel = self.selectedTreatWayModel.treatWay;
    [self.navigationController pushViewController:treatWayVC animated:YES];
}
#pragma mark - 通知传值
-(void)didSelectTreatWay:(NSNotification *)notification{
    KYTreatWayModel *selectModel = notification.userInfo[@"treatWayModel"];
    self.treatWayTextField.text = selectModel.treatWay;
    self.selectedTreatWayModel = selectModel;
}
#pragma mark - 申请按钮点击
- (IBAction)applyBtnKlick {
    KYMatchDoctorViewController *doctorVC = [[KYMatchDoctorViewController alloc]init];
    [self.navigationController pushViewController:doctorVC animated:YES];
}
#pragma mark - 得到匹配的医生的数量
-(void)getMatchDoctorCount{
    [KYMatchDoctorCountModel getMatchDoctorCountModelSuccess:^(NSString *count) {
        self.countLabel.text = count;
    }];
}
#pragma mark - 将要显示的时候
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if (self.diseareTextField.text.length == 0) {
        self.applyBtn.enabled = NO;
        return;
    }else{
        self.applyBtn.enabled = YES;
    }
    if (self.littleBtnThree.selected == NO) {
        self.applyBtn.enabled = YES;
    }else if (self.treatWayTextField.text.length == 0){
        self.applyBtn.enabled = NO;
    }else{
        self.applyBtn.enabled = YES;
    }
}
@end
